import React from 'react'

import { clearHighlight, createLayer, dataToFeature, geomToWkt } from '../../../../../../utils/helper/mapHelper';
import CztApi from '../../../../../../utils/apis/CztApi';
import JzzyApi from '../../../../../../utils/apis/JzzyApi';
import { resourcePath } from '../../../../../../utils/resource';
import { Icon, Style } from 'ol/style';
import { fromCircle } from 'ol/geom/Polygon';
import FxqyApi from '../../../../../../utils/apis/FxqyApi';
import CszhApi from '../../../../../../utils/apis/CszhApi';
import YhdApi from '../../../../../../utils/apis/YhdApi';

export default function rangeDisplayIcon(data, circle, map, ypType, wkt) {
    window.mmm = map;
    const allLayers = map.getLayers().getArray();
    if (wkt !== 'nowkt') {
        wkt = geomToWkt(wkt);
    }
    [...allLayers].forEach(l => {
        if (l?.get("layerName") === 'rangeDisplayIcon')
            map.removeLayer(l);
    })
    let Layer;
    console.log('mz', data);
    const h = (data, index) => {
        const e = data[index];
        if (!e) {
            return null;
        }
        const name = e.name;
        const handleResp = (resp, name) => {
            const features = resp.data?.filter(row => {
                return row?.geom
            })?.map(item => {
                const f = dataToFeature(item)
                return f;
            })?.filter(feature => feature.get('cztxl') === `${name}`) || []

            if (resourcePath?.iconMap?.[name]) {
                const layer = createLayer((feature) => {
                    // console.log(feature.get('_iconName'), resourcePath?.iconMap?.[feature.get('_iconName')])
                    return new Style({
                        image: new Icon({
                            src: resourcePath?.iconMap?.[name],
                            scale: 0.7,
                            anchor: [0.5, 1]
                            // size: [60, 98],  
                            // scale: 1 / 24
                        })
                    })
                }, features);
                layer.set('type', 'czt')
                layer.set('layerName', 'rangeDisplayIcon');
                map.addLayer(layer)
            }
        }
        if (ypType === 1 || ypType === 'czt') {
            CztApi.cztxq(name, fromCircle(circle)).then(resp => {
                handleResp(resp, name);
            }).catch(error => {
                console.error(error);
            }).finally(() => {
            });
        } else if (ypType === 'cztfx') {
            CztApi.cztxq(name, wkt).then(resp => {
                handleResp(resp, name);

            }).catch(error => {
                console.error(error);
            });
        } else if (ypType === 'jzzy') {
            CztApi.jzzyxlWkt(name, wkt).then(resp => {
                //接口需要优化
                let promises = resp?.data.map(e => {
                    return JzzyApi.listId(e.id).then(resp => {
                        return resp;
                    });
                });
                Promise.all(promises).then(responses => {
                    let geom = responses.map(resp => resp.data);
                    const mergedArray = [...geom.flat()];
                    //let Layer;
                    const features = mergedArray?.filter(row => {
                        return row?.geom
                    })?.map(item => {
                        return dataToFeature(item)
                    })
                    Layer = createLayer((feature) => {
                        return new Style({
                            image: new Icon({
                                src: resourcePath?.iconMap?.[`${name}`],
                                scale: 0.7,
                                anchor: [0.5, 1]
                                // size: [60, 98],  
                                // scale: 1 / 24
                            })
                        })
                    }, features?.filter(feature => feature.get('jzzyxl') === `${name}`) || []);
                    Layer.set('type', 'czt')
                    Layer.set('layerName', 'rangeDisplayIcon');
                    map.addLayer(Layer)
                }).catch(error => {
                    console.error(error);
                });
            }).catch(error => {
                console.error(error);
            });
        } else if (ypType === 'jzzypg') {
            CztApi.jzzyxlWkt_pol(name, fromCircle(circle)).then(resp => {
                //接口需要优化
                let promises = resp?.data.map(e => {
                    return JzzyApi.listId(e.id).then(resp => {
                        return resp;
                    });
                });
                Promise.all(promises).then(responses => {
                    let geom = responses.map(resp => resp.data);
                    const mergedArray = [...geom.flat()];
                    //let Layer;
                    const features = mergedArray?.filter(row => {
                        return row?.geom
                    })?.map(item => {
                        return dataToFeature(item)
                    })
                    Layer = createLayer((feature) => {
                        return new Style({
                            image: new Icon({
                                src: resourcePath?.iconMap?.[`${name}`],
                                scale: 0.7,
                                anchor: [0.5, 1]
                                // size: [60, 98],  
                                // scale: 1 / 24
                            })
                        })
                    }, features?.filter(feature => feature.get('jzzyxl') === `${name}`) || []);
                    Layer.set('type', 'czt')
                    Layer.set('layerName', 'rangeDisplayIcon');
                    map.addLayer(Layer)
                }).catch(error => {
                    console.error(error);
                });
            }).catch(error => {
                console.error(error);
            });

        } else if (ypType === 'yhd') {
            YhdApi.yhdxq(name, fromCircle(circle)).then(resp => {
                //接口需要优化
                let promises = resp.data.map(e => {
                    return YhdApi.listId(e.id).then(resp => {
                        return resp;
                    });
                });
                Promise.all(promises).then(responses => {

                    let geom = responses.map(resp => resp.rows);
                    const mergedArray = [...geom.flat()];

                    //let Layer;
                    const features = mergedArray?.filter(row => {
                        return row?.geom
                    })?.map(item => {
                        return dataToFeature(item)
                    })

                    Layer = createLayer((feature) => {
                        return new Style({
                            image: new Icon({
                                src: resourcePath?.iconMap?.[`${name}`],
                                scale: 0.7,
                                anchor: [0.5, 1]
                                // size: [60, 98],  
                                // scale: 1 / 24
                            })
                        })
                    }, features?.filter(feature => feature.get('yhlx') === `${name}`) || []);
                    Layer.set('type', 'yhd')
                    Layer.set('layerName', 'rangeDisplayIcon');
                    map.addLayer(Layer)
                    //console.log('center', mergedArray, features);

                }).catch(error => {
                    console.error(error);
                });
                //console.log("1",resp);
                //console.log("2",promises);
            }).catch(error => {
                console.error(error);
            });

        } else if (ypType === 'cszh') {
            CszhApi.cszhWkt(wkt, name).then(resp => {
                //接口需要优化
                let promises = resp.data.map(e => {
                    return CszhApi.listId(e.id).then(resp => {
                        return resp;
                    });
                });
                Promise.all(promises).then(responses => {
                    let geom = responses.map(resp => resp.rows);
                    const mergedArray = [...geom.flat()];
                    const features = mergedArray?.filter(row => {
                        return row?.geom
                    })?.map(item => {
                        return dataToFeature(item)
                    })
                    Layer = createLayer((feature) => {
                        return new Style({
                            image: new Icon({
                                src: resourcePath?.iconMap?.[`${name}`],
                                scale: 0.7,
                                anchor: [0.5, 1]
                                // size: [60, 98],  
                                // scale: 1 / 24
                            })
                        })
                    }, features?.filter(feature => feature.get('cszhlx') === `${name}`) || []);
                    Layer.set('type', 'czt')
                    Layer.set('layerName', 'rangeDisplayIcon');
                    map.addLayer(Layer)
                }).catch(error => {
                    console.error(error);
                })
            }).catch(error => {
                console.error(error);
            });
        } else if (ypType === 'zz') {
            FxqyApi.wkt_find(wkt, name).then(resp => {
                let promises = resp.data.map(e => {
                    return FxqyApi.yhdjId(e.id).then(resp => {
                        return resp;
                    });
                });
                Promise.all(promises).then(responses => {
                    let geom = responses.map(resp => resp.rows);
                    const mergedArray = [...geom.flat()];
                    const features = mergedArray?.filter(row => {
                        return row?.geom
                    })?.map(item => {
                        return dataToFeature(item)
                    })
                    Layer = createLayer((feature) => {
                        return new Style({
                            image: new Icon({
                                src: resourcePath?.iconMap?.[`${name}`],
                                scale: 0.7,
                                anchor: [0.5, 1]
                                // size: [60, 98],  
                                // scale: 1 / 24
                            })
                        })
                    }, features?.filter(feature => feature.get('yhlx') === `${name}`) || []);
                    Layer.set('type', 'czt')
                    Layer.set('layerName', 'rangeDisplayIcon');
                    map.addLayer(Layer)
                }).catch(error => {
                    console.error(error);
                });
            }).catch(error => {
                console.error(error);
            });
        } else {
            CztApi.jzzyxq(name, fromCircle(circle)).then(resp => {
                //接口需要优化
                let promises = resp.data.map(e => {
                    return JzzyApi.listId(e.id).then(resp => {
                        return resp;
                    });
                });

                Promise.all(promises).then(responses => {

                    let geom = responses.map(resp => resp.data);
                    const mergedArray = [...geom.flat()];
                    //let Layer;
                    const features = mergedArray?.filter(row => {
                        return row?.geom
                    })?.map(item => {
                        return dataToFeature(item)
                    })

                    Layer = createLayer((feature) => {
                        return new Style({
                            image: new Icon({
                                src: resourcePath?.iconMap?.[`${name}`],
                                scale: 0.7,
                                anchor: [0.5, 1]
                                // size: [60, 98],  
                                // scale: 1 / 24
                            })
                        })
                    }, features?.filter(feature => feature.get('jzzyxl') === `${name}`) || []);
                    Layer.set('type', 'czt')
                    Layer.set('layerName', 'rangeDisplayIcon');
                    map.addLayer(Layer)
                    //console.log('center', mergedArray,features);

                }).catch(error => {
                    console.error(error);
                });
                //console.log("1",resp);
                // console.log("2",promises);
            }).catch(error => {
                console.error(error);
            });
        }
        return null;
    }
    data.forEach((d, i) => {
        h(data, i)
    })
    return Layer;
}
